package com.hongkzh.www.shop.controller;

import com.auth0.jwt.interfaces.Claim;
import com.hongkzh.www.conmon.*;
import com.hongkzh.www.shop.service.AccountService;
import com.hongkzh.www.swagger.annotation.*;
import com.jfinal.aop.Clear;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;

import java.util.List;
import java.util.Map;

/**
 * @author martins
 */
@Api(tag = "account", description = "身份信息")
public class AccountController extends BaseController {

    /**
     * 上传文件基础虚拟路径
     */
    public static final String USERFILES_BASE_URL = "/userfiles/1/account/";
    AccountService accountService = new AccountService();


    @ApiOperation(url = "/v1/account/shopAccount", tag = "account", httpMethod = "post", description = "提现认证")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "bankNo", description = "银行卡号 BASE64加密", required = true, dataType = "string"),
            @Param(name = "cardNo", description = "身份证号码 BASE64加密", required = true, dataType = "string"),
            @Param(name = "payPawwrod", description = "支付密码 BASE64加密", required = true, dataType = "string"),
            @Param(name = "openingBank", description = "银行卡开户行", required = true, dataType = "string"),
            @Param(name = "openingName", description = "银行卡开户人姓名", required = true, dataType = "string"),
            @Param(name = "bankPhone", description = "开卡电话 BASE64加密", required = true, dataType = "string"),
            @Param(name = "bankPositiveImg", description = "银行卡正面照片", required = true, dataType = "string"),
            @Param(name = "banBackImg", description = "银行卡背面照片", required = true, dataType = "string"),
            @Param(name = "cardPositiveImg", description = "身份证正面照片", required = true, dataType = "string"),
            @Param(name = "cardBackImg", description = "身份证背面照片", required = true, dataType = "string"),
            @Param(name = "shopId", description = "自媒体用户ID 为null则取当前登录用户ID", required = false, dataType = "string"),
            @Param(name = "isUpdate", description = "是否修改 0-保存 1-修改", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = "{ loginUid     当前用户id\n" +
                            "bankNo     银行卡号\n" +
                            "cardNo     身份证号码\n" +
                            "openingBank     银行卡开户行\n" +
                            "openingName     银行卡开户人姓名\n" +
                            "bankPhone     开卡电话\n" +
                            "bankPositiveImg     银行卡正面照片\n" +
                            "banBackImg     银行卡背面照片\n" +
                            "cardPositiveImg     身份证正面照片\n" +
                            "cardBackImg     身份证背面照片\n" +
                            "shopId     自媒体用户ID\n" +
                            "createDate 创建时间 }"),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })
    @Clear
    public void shopAccount() {
        ReqResponse<String> r = new ReqResponse();
        boolean flag = false;
        List<UploadFile> fileList = getFiles();
        String token = getPara("loginUid");
        String loginUid = "";
        if (StrKit.notBlank(token)) {
            Map<String, Claim> map = JwtUtil.unsign(token);
            loginUid = map.get("user_id").asString();
        }
        if (StrKit.isBlank(loginUid)) {
            flag = true;
            r.setCode(1);
            r.setMsg("用户ID为空");
            renderJson(r);
        }
        String bankNo1 = getPara("bankNo");
        String bankNo = Encodes.decodeBase64String(bankNo1);
        if (StrKit.isBlank(bankNo)) {
            flag = true;
            r.setCode(1);
            r.setMsg("银行卡号为空");
            renderJson(r);
        }
        String cardNo1 = getPara("cardNo");
        String cardNo = Encodes.decodeBase64String(cardNo1);
        if (StrKit.isBlank(cardNo)) {
            flag = true;
            r.setCode(1);
            r.setMsg("身份证号码为空");
            renderJson(r);
        }
        String payPawwrod1 = getPara("payPawwrod");
        String payPawwrod = PasswordUtils.entryptPassword(Encodes.decodeBase64String(payPawwrod1));
        if (StrKit.isBlank(payPawwrod)) {
            flag = true;
            r.setCode(1);
            r.setMsg("支付密码为空");
            renderJson(r);
        }
        String openingBank = getPara("openingBank");
        if (StrKit.isBlank(openingBank)) {
            flag = true;
            r.setCode(1);
            r.setMsg("开户行名称为空");
            renderJson(r);
        }
        String openingName = getPara("openingName");
        if (StrKit.isBlank(openingName)) {
            flag = true;
            r.setCode(1);
            r.setMsg("开卡人为空");
            renderJson(r);
        }
        String bankPhone1 = getPara("bankPhone");
        String bankPhone = Encodes.decodeBase64String(bankPhone1);
        if (StrKit.isBlank(bankPhone)) {
            flag = true;
            r.setCode(1);
            r.setMsg("银行预留电话为空");
            renderJson(r);
        }

        String shopId = getPara("shopId");
        if (StrKit.isBlank(shopId)) {
            shopId = loginUid;
        }

        String bankPositiveImg = getPara("bankPositiveImg");
        String banBackImg = getPara("banBackImg");
        String cardPositiveImg = getPara("cardPositiveImg");
        String cardBackImg = getPara("cardBackImg");
        String src = null;
        for (UploadFile uf : fileList) {
            src = USERFILES_BASE_URL + DateUtil.FormatDateMMDD() + "/";
            switch (uf.getParameterName()) {
                case "bankPositiveImg":
                    bankPositiveImg = getFilePath(src, uf);
                    break;
                case "banBackImg":
                    banBackImg = getFilePath(src, uf);
                    break;
                case "cardPositiveImg":
                    cardPositiveImg = getFilePath(src, uf);
                    break;
                case "cardBackImg":
                    cardBackImg = getFilePath(src, uf);
                    break;
                default:
                    break;
            }
        }

        if (StrKit.isBlank(bankPositiveImg)) {
            flag = true;
            r.setCode(1);
            r.setMsg("银行卡照片为空");
            renderJson(r);
        }

        if (StrKit.isBlank(banBackImg)) {
            flag = true;
            r.setCode(1);
            r.setMsg("银行卡照片为空");
            renderJson(r);
        }

        if (StrKit.isBlank(cardPositiveImg)) {
            flag = true;
            r.setCode(1);
            r.setMsg("身份证照片为空");
            renderJson(r);
        }

        if (StrKit.isBlank(cardBackImg)) {
            flag = true;
            r.setCode(1);
            r.setMsg("身份证照片为空");
            renderJson(r);
        }

        String isUpdate = getPara("isUpdate");
        if (StrKit.isBlank(isUpdate)) {
            flag = true;
            r.setCode(1);
            r.setMsg("是否修改参数为空");
            renderJson(r);
        }
        String id = IdGen.uuid();
        int result = 0;
        try {
            if (!flag) {
                result = accountService.saveAccount(id, loginUid, bankNo, cardNo, payPawwrod, openingBank, openingName, bankPhone, bankPositiveImg, banBackImg, cardPositiveImg, cardBackImg, shopId, isUpdate);
            }
        } catch (Exception e) {
            r.setCode(1);
            r.setMsg("保存信息失败");
            e.printStackTrace();
        }
        r.setData(result + "");
        renderJson(r);
    }


    @ApiOperation(url = "/v1/account/applyForWithdrawal", tag = "account", httpMethod = "post", description = "申请提现")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "orderNumber", description = "订单号", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = "{ }"),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })
    @Clear
    public void applyForWithdrawal() {
        ReqResponse<String> r = new ReqResponse();
        boolean flag = false;

        String token = getPara("loginUid");
        String loginUid = "";
        if (StrKit.notBlank(token)) {
            Map<String, Claim> map = JwtUtil.unsign(token);
            loginUid = map.get("user_id").asString();
        }
        String orderNumber = getPara("orderNumber");
        if (StrKit.isBlank(loginUid)) {
            flag = true;
            r.setCode(1);
            r.setMsg("用户ID为空");
            renderJson(r);
        }
        if (StrKit.isBlank(orderNumber)) {
            flag = true;
            r.setCode(1);
            r.setMsg("订单号为空");
            renderJson(r);
        }
        int result = 0;
        try {
            result = accountService.applyForWithdrawal(loginUid, orderNumber);
        } catch (Exception e) {
            r.setCode(1);
            r.setMsg("申请提现失败");
            e.printStackTrace();
        }
        r.setData(result + "");
        renderJson(r);
    }

    @ApiOperation(url = "/v1/account/withdrawalInformation", tag = "account", httpMethod = "post", description = "银行卡列表")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = "{ bankNo　银行卡号" +
                            "cardNo　身份证号" +
                            "openingBank　开户行" +
                            "openingName 开卡人姓名" +
                            "bankPhone 银行预留电化" +
                            "bankPositiveImg 银行卡正面" +
                            "bankBackImg  银行卡背面" +
                            "cardPositiveImg 身份证正面" +
                            "cardBackImg  身份背面 }"),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })
    public void withdrawalInformation() {
        ReqResponse<Object> r = new ReqResponse();
        boolean flag = false;

        String token = getPara("loginUid");
        String loginUid = "";
        if (StrKit.notBlank(token)) {
            Map<String, Claim> map = JwtUtil.unsign(token);
            loginUid = map.get("user_id").asString();
        }
        if (StrKit.isBlank(loginUid)) {
            flag = true;
            r.setCode(1);
            r.setMsg("用户ID为空");
            renderJson(r);
        }
        try {
            List<Record> list = accountService.withdrawalInformation(loginUid);
            r.setData(list);
        } catch (Exception e) {
            r.setCode(1);
            r.setMsg("获取数据失败");
            e.printStackTrace();
        }
        renderJson(r);
    }


}
